/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2021-08-20 14:05:47
 * @LastEditors: ding
 * @LastEditTime: 2022-09-26 18:27:02
 * @Description: 海清机器，时间、重启、等设备设置接口的封装tools工具
 */
import { actions, } from "../../http/actions";
import { hc } from "../../hc";
import HCMqttBaseService from "../BaseService";


/** 海清时间处理等常用工具 */
export default class MQTTMachineToolsService extends HCMqttBaseService {

	/**
	 *  获取设备系统时间
	 *
	 * @param {string} topic 设备的话题，默认一般为mqtt/face/ID 其中ID为设备id
	 * @return {*}
	 * @memberof MQTTMachineToolsService
	 */
	async getSysTime(topic: string) {
		const operator = actions.GetSysTime;
		const client = this.getMQTTClient();

		const res = await client.fetch<hc.mqtt.getSysTimeAck>(
			topic,
			{
				operator,
				info: {},
			},
			{ qos: 1, retain: false }
		);
		const deviceTime = res.message.info;
		// deviceTime.SysTime = this.app.tools.DateFormat.init(deviceTime.SysTime).format();
		const response = {
			...deviceTime,
			deviceId: deviceTime.facesluiceId,
		};
		return response;
	}

	/**
	 *  设置系统时间
	 *
	 * @param {string} topic
	 * @param {(string)} [time] 时间 eg: yyyy-MM-ddThh:mm:ss
	 * @return {*}
	 * @memberof MQTTMachineToolsService
	 */
	async setSysTime(topic: string, time: string) {
		const operator = actions.SetSysTime;
		const client = this.getMQTTClient();
		const res = await client.fetch<hc.mqtt.setSysTimeAck>(
			topic,
			{
				operator,
				info: {
					SysTime: time,
				},
			},
			{ qos: 1, retain: false }
		);

		return res;
	}

	/** 重启设备 */
	async rebootDevice(topic: string) {
		const op = actions.RebootDevice;
		const data = {
			operator: op,
			info: {},
		};
		const client = this.getMQTTClient();
		const res = await client.fetch<hc.mqtt.rebootDeviceAck>(topic, data, { qos: 1, retain: false });
		return res;
	}

	/**
	 *
	 * 远程开门
	 * @param {string} topic
	 * @param {(0 | 1)} openDoor 0不开门 1开门
	 * @param {string} showInfo  UI显示内容
	 * @return {*}
	 * @memberof MQTTMachineToolsService
	 */
	async openDoor(topic: string, openDoor: 0 | 1, showInfo: string) {
		const op = actions.Unlock;
		const data = {
			operator: op,
			info: {
				uid: "0",
				openDoor,
				showInfo,
			},
		};
		const client = this.getMQTTClient();
		const res = await client.fetch<hc.mqtt.unlockAck>(topic, data, { qos: 1, retain: false });
		return res;
	}
}
